//
//  MainDataHelper.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/12.
//  Copyright (c) 2015年 王丽珍. All rights reserved.
//

#import "MainDataHelper.h"

@implementation MainDataHelper

+ (void)postDataWithUrlString:(NSString *)string
                andBodyString:(NSString *)bodyString
                      compare:(MyBlock)block
{
    //对做一次转码，防止参数里有中文
    NSString *urlString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data != nil) {
            //因为不确定数据的类型，所以用id泛型指针去接收
            id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            block(object);
        }
    }];
    
}

@end
